package model;

import entity.Book;
import entity.Orders;
import entity.OrdersDetails;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import manager.Manager;

/**
 *
 * @author Noi
 */
public class BookToPayTableModel extends AbstractTableModel {

    private ArrayList<OrdersDetails> alOrdersDetails;
    private manager.Manager manager;

    public BookToPayTableModel(Manager manager) {
        this.alOrdersDetails = new ArrayList<OrdersDetails>();
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        return alOrdersDetails.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    private int getExceptBetweenDays(Date duedate, Date today) {
        Calendar dueCalendar = Calendar.getInstance();
        dueCalendar.setTime(duedate);
        Calendar todayCalendar = Calendar.getInstance();
        todayCalendar.setTime(today);
        int num = 0;
        if(dueCalendar.getTimeInMillis() >= todayCalendar.getTimeInMillis()) return 0;
        while (dueCalendar.get(Calendar.DAY_OF_YEAR) != todayCalendar.get(Calendar.DAY_OF_YEAR)) {
            num++;
            dueCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return num;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        OrdersDetails details = this.alOrdersDetails.get(rowIndex);
        Orders orders = this.manager.getOrdersManager().getByOrdersID(details.getOrdersID());
        Book book = this.manager.getBookManager().getByBookID(details.getBookID());

        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();

        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        int d = this.getExceptBetweenDays(orders.getDueDate(), today);

        switch (columnIndex) {
            case 0:
                return book;
            case 1:
                return df.format(orders.getDueDate());
            case 2:
                return d;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Book Name";
            case 1:
                return "Due Date";
            case 2:
                return "The amount payable (cent)";
            default:
                return null;
        }
    }

    public int add(OrdersDetails details) {
        int count = 0;
        
        this.alOrdersDetails.add(details);
        this.fireTableDataChanged();
        
        for(int i = 0; i < this.alOrdersDetails.size(); i++) {
            count += Integer.valueOf(this.getValueAt(i, 2).toString());
        }
        return count;
    }

    public int remove(int index) {
        int count = 0;
        
        this.alOrdersDetails.remove(index);
        this.fireTableDataChanged();
        
        for(int i = 0; i < this.alOrdersDetails.size(); i++) {
            count += Integer.valueOf(this.getValueAt(i, 2).toString());
        }
        return count;
    }

    public OrdersDetails get(int index) {
        return this.alOrdersDetails.get(index);
    }
    
    public int size() {
        return this.alOrdersDetails.size();
    }
    
    public void clear() {
        this.alOrdersDetails.clear();
        this.fireTableDataChanged();
    }
}